package com.zzy.sta.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zzy.commonutils.R;
import com.zzy.commonutils.ordervo.UcenterMemberOrder;
import com.zzy.sta.client.UcenterClient;
import com.zzy.sta.entity.StatisticsDaily;
import com.zzy.sta.mapper.StatisticsDailyMapper;
import com.zzy.sta.service.StatisticsDailyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author KAZU
 * @since 2023-01-25
 */
@Service
public class StatisticsDailyServiceImpl extends ServiceImpl<StatisticsDailyMapper, StatisticsDaily> implements StatisticsDailyService {
	
	@Autowired
	private UcenterClient ucenterClient;
	
	@Autowired
	private StatisticsDailyMapper dailyMapper;
	
	/**
	 * 统计某一天的注册人数
	 * @param day
	 * */
	@Override
	public void registerCount(String day) {
		// 添加记录之前删除表中和相同日期数据
		QueryWrapper<StatisticsDaily> wrapper = new QueryWrapper<>();
		wrapper.eq("date_calculated", day);
		dailyMapper.delete(wrapper);
		
		// 远程调用得到某一天注册人数
		R registerR = ucenterClient.countRegister(day);
		Integer countRegister = (Integer) registerR.getData().get("countRegister");
		// 把获取到的数据添加到数据库，的统计分析表中
		StatisticsDaily daily = new StatisticsDaily();
		// 注册人数
		daily.setRegisterNum(countRegister);
		daily.setDateCalculated(day);
		// 随机数
		daily.setVideoViewNum(RandomUtils.nextInt(100, 200));
		daily.setLoginNum(RandomUtils.nextInt(100, 200));
		daily.setCourseNum(RandomUtils.nextInt(100, 200));
		dailyMapper.insert(daily);
	}
	
	/**
	 * 图表显示，返回两部分数据，日期json数组，数量json数组
	 * @return
	 * @param type
	 * @param begin
	 * @param end
	 * */
	@Override
	public Map<String, Object> getShowData(String type, String begin, String end) {
		// 根据条件查询对应数据
		QueryWrapper<StatisticsDaily> wrapper = new QueryWrapper<>();
		wrapper.between("date_calculated", begin, end);
		wrapper.select("date_calculated", type);
		List<StatisticsDaily> dailyList = dailyMapper.selectList(wrapper);
		// 返回的有两部分数据，日期和日期对应数量
		// 前端要求json数组结构，对应后端java代码为list集合
		List<String> dataList = new ArrayList<>();
		List<Integer> numDataList = new ArrayList<>();
		// 遍历查询所有数据list集合，进行封装
		for (StatisticsDaily daily : dailyList) {
			// 封装日期list
			dataList.add(daily.getDateCalculated());
			// 封装对应数量list
			switch (type) {
				case "login_num":
					numDataList.add(daily.getLoginNum());
					break;
				case "register_num":
					numDataList.add(daily.getRegisterNum());
					break;
				case "video_view_num":
					numDataList.add(daily.getVideoViewNum());
					break;
				case "course_num":
					numDataList.add(daily.getCourseNum());
					break;
				default:
					break;
			}
		}
		// 把封装之后的两个list集合，封装到map并返回
		HashMap<String, Object> map = new HashMap<>();
		map.put("dataList", dataList);
		map.put("numDataList", numDataList);
		return map;
	}
}

















